/*
* 点亮LED灯
*
*/

#define GPJ0BASE                        0xE0200000
#define _GPJ0OFFSET(offset)             (GPJ0BASE + (offset))
#define GPJ0CON_LED                     _GPJ0OFFSET(0x0240)
#define GPJ0DAT_LED                     _GPJ0OFFSET(0x0244) 

#define GPD0BASE                        0xE0200000
#define _GPD0OFFSET(offset)             (GPD0BASE + (offset))
#define GPD0CON_LED                     _GPD0OFFSET(0x00A0)
#define GPD0DAT_LED                     _GPD0OFFSET(0x00A4) 

#define MODE_OUTPUT                     0b0001
#define LED_ON                          0b0
#define LED_OFF                         0b1



     .globl _start
_start:
    // GPJ0CON_[3, 4, 5] 设置为输出模式
    ldr r0, =GPJ0CON_LED 
    ldr r1, [r0]
    ldr r2, =(0xFFF << 12)
    bic r1, r1, r2
    ldr r2, =(0x111 << 12)
    orr r1, r1, r2 
    str r1, [r0]

    // GPJ0DAT_[3, 4, 5] 高电平，灭灯
    ldr r0, =GPJ0DAT_LED // GPJ0DAT
    ldr r1, [r0]
    orr r1, r1, #(0b111 << 3)
    str r1, [r0]

    // GPD0CON_1 设置为输出模式
    ldr r0, =GPD0CON_LED
    ldr r1, [r0]
    bic r1, r1, #(0xF << 4)
    orr r1, r1, #(MODE_OUTPUT << 4)
    str r1, [r0]

    // 灭掉第四个LED灯
    ldr r0, =GPD0DAT_LED
    ldr r1, [r0]
    orr r1, r1, #(0b1 << 1)
    str r1, [r0]

    // 保存灯的索引，0,1,2,3分别代表第 1， 2， 3， 4 盏 LED 灯
    ldr r0, =0
    // 第四站灯的索引，特殊处理
    ldr r7, =3

led_loop:
    // 如果是第四个LED灯，即GPD0CON控制的灯
    cmp r0, r7 
    // 跳转到 GPD0CON去控制第四个LED灯
    beq light_gpd0_led

    ldr r1, =GPJ0DAT_LED // GPJ0DAT
    ldr r2, [r1]

    // 最低偏移三位，3,4,5 偏移量分别代表 GPJ0DATA 对应的 3,4,5LED灯
    // r0 不断增加，0 - 2 就实现了 3,4,5 的偏移
    ldr r3, =3
    // 增加偏移量
    add r3, r3, r0

    ldr r4, =0b1
    // 左移
    mov r4, r4, lsl r3
    // 清零点亮
    bic r2, r2, r4
    str r2, [r1]

    bl delay 
    // 置1 关灯
    orr r2, r2, r4
    str r2, [r1]

    // 加1进下一次循环，加到 3 之后会变为 0
    add r0, r0, #1

    b led_loop
// GPD0 控制的灯
light_gpd0_led:
    // GPD0DAT_1 高电平，灭灯，低电平，开灯
    ldr r1, =GPD0DAT_LED
    ldr r2, [r1]
    bic r2, r2, #(0b1 << 1)
    str r2, [r1]
    
    bl delay 

    // 灭灯
    ldr r1, =GPD0DAT_LED
    ldr r2, [r1]
    orr r2, r2, #(0b1 << 1)
    str r2, [r1]

    bl delay 

    ldr r0, =0

    b led_loop

loop:
    b loop


delay: 
    ldr r5, =0x800000
    ldr r6, =0x0
delay_loop:
    sub r5, r5, #1
    cmp r5, r6
    bne delay_loop
    mov pc, lr
